package com.aaa.controller;

import com.aaa.pojo.DefaultMsg;
import com.aaa.service.SettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/uploader")
public class SettingController {
    @PostMapping("/picture")
    public DefaultMsg uploader(MultipartFile file) {
        DefaultMsg defaultMsg;
        String savePath = "E:/project_3/picture/";
        String newFileName = UUID.randomUUID().toString();
        File newPicture = new File(savePath + newFileName+".jpg");
        try {
            file.transferTo(newPicture);
            defaultMsg = new DefaultMsg(200, "上传成功", newFileName, "");
        } catch (IOException e) {
            defaultMsg = new DefaultMsg(500, "上传失败", null, "");
//            throw new RuntimeException(e);
            return defaultMsg;
        }
        return defaultMsg;
    }
}
